#pragma once
#include"RBTree.h"

namespace xlf
{
	template<class K,class V>
	class Map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)//仿函数取first
			{
				return kv.first;
			}
		};

	public:
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::Iterator iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;

		iterator begin()
		{
			return _t.Begin();
		}

		iterator end()
		{
			return _t.End();
		}
		
		const_iterator begin() const
		{
			return _t.Begin();
		}

		const_iterator end() const
		{
			return _t.End();
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _t.Insert(kv);
		}

		iterator find(const K& key)
		{
			return _t.Find(key);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
		}

	private:
		//RBTree<K, pair<K, V>, MapKeyOfT> _t;//不能修改first,可以修改second
		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
	};
	
	void test_map()
	{
		Map<string, string> m;
		m.insert({ "sort","排序" });
		m.insert({ "left","左边" });
		m.insert({ "right","右边" });
		m.insert({ "string","字符串" });
		m.insert({ "char","字符" });

		m["left"] = "左边，剩余";
		m["insert"] = "插入";
		m["string"];

		Map<string, string>::iterator it = m.begin();
		while (it != m.end())
		{
			//不能修改first,可以修改second
			//it->first += 'x';
			it->second += 'x';

			cout << it->first << ":" << it->second << endl;;
			++it;
		}
		cout << endl;

	}
}